;Copyright 2007-2010 Niko Carpenter and Tyler Spivey
;    This file is part of Linna Home Server.
;    Linna Home is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    any later version.

;    Linna Home is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with Linna Home.  If not, see http://www.gnu.org/licenses/.

#Include-once
global $refilter = ".*"
global $filterFlag = 1
addcmd("echo","echo's text back to the user. Usage: echo <text>")
func lin_echo($id,$str)
dSend($id,$str)
EndFunc
AddCmd("Info","Displays info about the user's computer.")
Func lin_Info($id,$ar)
$mem = memgetstats()
dSend($id,"Info:"&@lf&"User name: "&@Username&"."&@LF&"Computername: "&@Computername&"."&@LF&"OS: "&@OSVersion&" "&@OSServicePack&" build "&@OSBuild&"."&@LF&"Current system time/date: "&@Hour&":"&@MIN&":"&@Sec&".   "&@MON&"/"&@MDAY&"/"&@YEAR&"."&@LF&"OS language: "&_getoslang()&"."&@LF&"Processor architecture: "&@Processorarch&"."&@LF&"Memory usage: "&$mem[0]&"%."&@lf&"Internal IP addresses: "&_ShowInternalIPs()&"."&@lf)
if IsAdmin() Then
dSend($id,@Username&" is an admin.")
Else
dSend($id,@Username&" is not an admin.")
EndIf
dsend($id,"Linna location: "&@ScriptFullPath&@lf&" size: "&filegetsize(@ScriptFullpath)&" bytes.")
EndFunc
Func lin_kill($id,$str)
if $str <> "" Then
LinnaShutDown($ID, "Manual kill: "&$str)
Else
LinnaShutDown($ID, "Command ""kill"" was executed.")
EndIf
EndFunc
	
AddCMD("CMD","Simple access to command prompt. Usage: cmd <command>"&@lf&"When a program starts, you will see the PID of it along with it's out put. You will see it's output until it dies. When a program dies, you will be notified.")
func lin_cmd ($id, $str)
$El = -1
for $i = 0 to uBound($Processes)-1
if $Processes[$i][0] = -1 Then
$El = $i
ExitLoop
EndIf
Next
if $el = -1 Then
redim $Processes[ubound($Processes)+1][3]
$el = UBound($Processes)-1
EndIf
$Processes[$el][0] = $id
$Processes[$El][2] = $str
$Processes[$el][1] = run(@comspec & " /C "&$str,"",@SW_HIDE,7)
dsend($id,"PID: "&$Processes[$El][1])
endfunc
AddCMD("run","Runs a program. Usage: run <command>"&@lf&"When a program starts, you will see the PID of it along with it's out put. You will see it's output until it dies. When a program dies, you will be notified."&@lf&"If the file name contains spaces, you must surround the file name in quotes.")
func lin_run ($id, $str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"Runs a program. Usage: run <command>"&@lf&"When a program starts, you will see the PID of it along with it's out put. You will see it's output until it dies. When a program dies, you will be notified."&@lf&"If the file name contains spaces, you must surround the file name in quotes.")
Return
EndIf
$PID = run($str,"","",7)
if @Error Then
dSend($id,"Cannot run "&$str&".")
Return
EndIf
$El = -1
for $i = 0 to uBound($Processes)-1
if $Processes[$i][0] = -1 Then
$El = $i
ExitLoop
EndIf
Next
if $el = -1 Then
redim $Processes[ubound($Processes)+1][3]
$el = UBound($Processes)-1
EndIf
$Processes[$el][0] = $id
$Processes[$El][2] = $str
$Processes[$el][1] = $PID
dsend($id,"PID: "&$Processes[$El][1])
return $Processes[$El][1]
endfunc
AddCMD("runh","Runs a program (hidden). Usage: run <command>"&@lf&"When a program starts, you will see the PID of it along with it's out put. You will see it's output until it dies. When a program dies, you will be notified."&@lf&"If the file name contains spaces, you must surround the file name in quotes.")
func lin_runh ($id, $str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"Runs a program (hidden). Usage: run <command>"&@lf&"When a program starts, you will see the PID of it along with it's out put. You will see it's output until it dies. When a program dies, you will be notified."&@lf&"If the file name contains spaces, you must surround the file name in quotes.")
Return
EndIf
$PID = run($str,"","",7)
if @Error Then
dSend($id,"Cannot run "&$str&".")
Return
EndIf
$El = -1
for $i = 0 to uBound($Processes)-1
if $Processes[$i][0] = -1 Then
$El = $i
ExitLoop
EndIf
Next
if $el = -1 Then
redim $Processes[ubound($Processes)+1][3]
$el = UBound($Processes)-1
EndIf
$Processes[$el][0] = $id
$Processes[$El][2] = $str
$Processes[$el][1] = $PID
dsend($id,"PID: "&$Processes[$El][1])
endfunc
addCmd("Win","Displays the title of the user's current window and the text that can be found in it."&@lf&"Usage: win [-t|-x|-p]."&@lf&"Just this command alone will display the window title and text of the currently opened window. -t will only display the title, -x will only display the text and -p will display the process name and PID that the window belongs too.")
func lin_win($id,$str)
if $str = "-t" Then
dSend($id,"Window Title: "&WinGetTitle(""))
ElseIf $str = "-x" Then
dSend($id,"Window Text: "&WinGetText(""))
ElseIf $str = "-p" Then
$PID = WinGetProcess("")
$pList = ProcessList()
if @Error Then
dSend($id,"Window Process ID: "&$PID)
Else
$pname = ""
for $i = 1 To $PList[0][0]
if $PID = $pList[$i][1] Then
$PName = $PList[$i][0]
ExitLoop
EndIf
Next
if $pname = "" Then
dSend($id,"Window Process ID: "&$PID)
Else
dSend($id,"Window Process name: "&$Pname&" Process ID: "&$PID)
EndIf
EndIf
Else
dSend($ID,"Window Title: "&WinGetTitle("")&@lf&"Text: "&WinGetText(""))
EndIf
endfunc
addcmd("cat","Reads from a file. Usage: cat <filename>")
func lin_cat($id,$str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"cat: No file to read")
Return
EndIf

if fileExists($str) and StringRegExp($str,"^com\d*$",0) < 1 and stringregexp($str,"\\com\d*",0) < 1 then
dsend($id,"cat: contents of "&$str&@lf&fileRead($str))
Else
dSend($id,"cat: "&$str&": File doesn't exist")
endif

endfunc
addcmd("block","Blocks or unblocks user's input. Usage: block <option>. Options: on=block input, off=block input.")
func lin_block($id,$str)
local $Done = 0
if $str = "on" then
$blocked = 1
$Done = 1
elseIf $str = "off" Then
$blocked = 0
BlockInput(0)
$Done = 1
endif
if $Done = 1 Then
dsend($id,$str)
Else
dSend($ID,"Error. Invalid parameters. Type help block for more information.")
EndIf
endfunc

Addcmd("finfo","finfo: Gets information about a file. Usage: finfo <file>")
Func lin_finfo($id,$str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"Usage: finfo <file>")
return
EndIf
if not FileExists($str) Then
dSend($id,"finfo: "&$str&": File doesn't exist")
return
EndIf
dSend($id,"finfo: "&$str&": Attributes: "&FileGetAttrib($str)&" Modified: "&FileGetTime($str,0,0)&" Created: "&FileGetTime($str,0,1)&" Accessed: "&FileGetTime($str,0,2)&" Size: "&FileGetSize($str))
EndFunc
addcmd("clipget","clipget: Displays text on the clipboard.")
Func lin_clipget($id,$str)
$txt = ClipGet()
if @Error = 0 Then
dSend($id,"Clipget: The text on the clipboard is: "&$TXT)
ElseIf @Error = 1 Then
dSend($id,"Clipget: nothing is on the clipboard.")
ElseIf @Error = 2 Then
dSend($id,"clipget: The data on the clipboard is not text")
Else
dSend($id,"Clipget: Cannot access the clipboard")
EndIf
EndFunc
addcmd("varset","varset: Sets an environment variable. Usage: varset <var>=[text]."&@lf&" To set a variable blank, just don't put any text.")
Func lin_varset($id,$str)
$pos = StringInStr($str,"=")
if $Pos <= 1 Then
dSend($id,"varset: "&$str&": format not correct.")
return
EndIf
$var = StringLeft($str,$pos-1)
$str = StringTrimLeft($str,StringLen($var)+1)
if EnvSet($var,$str) Then
dSend($id,"varset: "&$var&" is set to "&$str)
Else
dSend($id,"varset: "&$var&" cannot be set to "&$str)
EndIf
EndFunc
addcmd("varget","varget: Gets the value of an environment variable. Usage: varget <variable>")
Func lin_varget($id,$str)
if $str = "" Then
dSend($id,"varget: Format not correct")
Else
dSend($id,"varget: "&$str&"="&EnvGet($str))
EndIf
EndFunc
addcmd("cd","Changes to a different directory. Usage: cd <path>")
Func lin_cd($id,$str)
if $str = "" Then $str = "~"
$str = stringreplace($str,"/","\")
if stringright($str,1) <> "\" Then $str &= "\"
$str = StringRegExpReplace($str,"^~",StringReplace(@ScriptDir,"\","\\"))
local $dir = 0
$dir=FileChangeDir($str)
If $dir=1 Then
if not $V_Type = "server" Then dsend($id,@WorkingDir&">")
Else
dsend($id,"Cannot change to "&$str)
if not $V_Type = "Server" Then dSend($Id,@WorkingDir&">")
EndIf
EndFunc
addcmd("pwd","Displays working directory.")
Func lin_pwd($id,$str)
dsend($id,@WorkingDir)
EndFunc
addcmd("gdl","Get's the list of drives on a user's computer. Usage: gdl [type]. Leave blank or set to all to see all drives. Some types include all, fixed, mounted...")
Func lin_gdl($id,$str)
dim $drives
if $str="" Then $str = "all"
$drives=DriveGetDrive($str)
If $drives<>"" Then
dSend($id,"Current drive list showing " & $str&" drives")
For $count=1 to $drives[0]
dSend($id,DriveGetLabel($drives[$count]) & " (" & $drives[$count] & ") " & DriveGetType($Drives[$count]) & " FS: " & DriveGetFileSystem($drives[$count]) & " Used: " & Round((DriveSpaceTotal($Drives[$count])-DriveSpaceFree($drives[$count]))/1024,2) & "/" & Round(DriveSpaceTotal($drives[$count])/1024,2) & " GB. Status: "&DriveStatus($drives[$count]))
Next
dSend($id,"End of Drive List.")
Else
dSend($id,"error in getting list of drives")
EndIf
EndFunc
AddCmd("cdtray","Opens or closes the specified cd drive. usage: cdtray <drive:> <status> (Status can be ""open"" or ""closed"".")
Func lin_Cdtray($id,$str)
	$arz = StringRegexp($str,"^([a-zA-Z]:)\s+(open|closed)$",1)
	if not @Error Then
$a = $arz[0]
$b = $arz[1]
$res = 		CDTray($a,$b)
if $res = 0 Then
dSend($id,"Could not set the status of drive "&$a&" to "&$b&". ither that drive is not a cd drive or it is locke by another program such as a cd burner.")
Else
dSend($id,"Drive "&$a&" is now "&$b&".")
EndIf
Else
dSend($id,"usage: cdtray <drive:> <status>. You may set status to either ""open"" or ""closed"".")
EndIf
EndFunc
addcmd("key","Sends a hotkey to the user's computer. Usage: key <hotkey>")
Func lin_Key($id,$str)
	if $str <> "" Then
		Send($str)
	if @Error Then
		dSend($id,"That hotkey could not be sent.")
		Else
		dSend($id,"The hotkey was sent.")
	EndIf
	Else
	dSend($id,"Error. You must specify a hotkey to send.")
	EndIf
	EndFunc
	addcmd("shutdown","Shutdown a user's system. Usage: shutdown <flag>.")
Func lin_Shutdown($id,$str)
$ret = 0
if StringIsInt($str) Then $ret = Shutdown($str)
if $ret = 1 Then
dSend($Id,"The shutdown command was successful")
Else
dSend($id,"The shutdown command was not successful.")
EndIf
EndFunc
addcmd("chmod","chmod: Sets attributes for files and directories. Usage: chmod <file>|<attribs>. Attributes can be R, A, S, H, T, O, and N. "&@lf&" (Note that you cannot set the compressed/directory attributes with this command.)")
Func lin_chmod($id,$str)
$split = StringSplit($str,"|")
if $split[1] = "" or StringRegExp($split[1],"^com\d*$",0) or stringregexp($split[1],"\\com\d*",0) or $split[0] <> 2 Then
dSend($id,"chmod: Format not correct.")
return
EndIf
$Split[2] = StringUpper($Split[2])
FileSetAttrib($Split[1], $Split[2],1)
$attribs = FileGetAttrib($Split[1])
if $attribs = "" Then
dSend($id,"chmod: "&$split[1]&": Could not get resulting attributes")
Else
dSend($id,"chmod: "&$Split[1]&": Attributes: "&$attribs)
EndIf

EndFunc
addcmd("rm","Deletes a file. Usage: rm <file>")
Func lin_rm($id,$str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"Usage: rm <file>")
return
EndIf
If FileExists($str) Then
If FileDelete($str) Then
dsend($id,"rm: "&$str&": removed")
Else
dsend($id,"rm: "&$str&": Cannot remove file")
EndIf
Else
dsend($id,"rm: "&$str&": File doesn't exist")
EndIf
EndFunc
addcmd("mkdir","mkdir: Makes a directory. Usage: mkdir <directory>")
Func lin_mkdir($id,$str)
if DirCreate($str) Then
dSend($id,"mkdir: "&$str&": Directory created.")
Else
dSend($id,"mkdir: "&$str&": Cannot create directory")
EndIf
EndFunc
addcmd("rmdir","Removes a directory. Usage: rmdir <directory>")
Func lin_rmdir($id,$str)
if DirGetSize($str,2) = -1 Then
dSend($id,"rmdir: "&$str&": No such file or directory")
Else
if not DirRemove($str) Then
dSend($ID,"rmdir: "&$str&": Unable to remove that directory.")
Else
dSend($ID,"Rmdir: "&$str&": Directory removed successfully")
EndIf
EndIf
EndFunc
addcmd("rpl","Get the list of running processes. Type rpl <processname> to check if the vien process exists without showing the entire list.")
Func lin_RPL($id,$str)
if $str <> "" Then
if processExists($str) Then
dSend($id,$str&" is running.")
Else
dSend($id,$str&" is not running.")
EndIf
Else
dSend($id,"beginning of process list")
$array=ProcessList()
For $count=1 to $array[0][0]
dSend($id,"Process " & $count & ": " & $array[$count][0] & " PID: " & $array[$count][1] & @LF)
Next
dSend($id,"End of Process List")
EndIf
EndFunc
Addcmd("end","Ends a process. Usage end <process>. Process can be a process name like ""taskmgr.exe"" or a PID like ""3992"".")
Func lin_End($id,$str)
if $str = "" Then $str = "(no process entered)"
if processExists($str) Then
ProcessClose($str)
dSend($id,$str&" was ended successfully.")
Else
dSend($id,"The process "&$str&" does not exist.")
EndIf
EndFunc

addcmd("beep","Uses the PC speaker to beep. Usage: beep [frequency] [time]. The frequency is in hertz and the time is in miliseconds. The default frequency is 1000 hz and the default time is 1000 ms You can only make a computer beep for 10000 ms..")
Func lin_Beep($id,$str)
$arz = StringRegExp($str,"^(\d+)\s+(\d+)$",1)
if not @Error Then
if $arz[1] > 10000 Then $arz[1] = 10000
dSend($id,"Beeping at "&$arz[0]&" hz for "&$arz[1]&" ms.")
Beep($arz[0],$arz[1])
dSend($id,"Finished beeping.")
Else
dSend($id,"Beeping at 1000 hz for 1000 ms.")
Beep()
dSend($id,"Finished beeping.")
EndIf
EndFunc
addCmd("play","plays a sound with soundPlay.")
func lin_play($id,$str)
if $str <> "" Then
if fileExists($str) then
soundPlay($str)
dSend($id,"playing "&$str)
else
dSend($id,"Cannot find "&$str)
endif
Else
SoundPlay("")
dsend($id,"stopped.")
EndIf
endfunc
AddCmd("idle","returns the idle time of the user.")
func lin_idle($id,$str)
$lastinputinfoStruct = dllStructCreate("uint;dword")
dllStructSetData($lastinputinfoStruct,1,dllStructGetSize($lastinputinfoStruct))
dllCall("user32.dll","int","GetLastInputInfo","ptr",dllStructGetPtr($lastinputinfoStruct))
$lastActiveInTicks = dllStructGetData($lastinputinfoStruct,2)
$hours = 0
$minutes = 0
$seconds = 0
$currentTicks = dllCall("kernel32.dll","long","GetTickCount")
_ticksToTime($currentTicks[0]-$lastActiveInTicks,$hours,$minutes,$seconds)
dSend($id, "idle for "&$hours&":"&$minutes&":"&$seconds)
endFunc
addCmd("say","says something. say <jfw|we|sapi>=text")
func lin_say($id,$str)
$delimiter = stringInStr($str,"=")
if $delimiter = 0 then
dSend($id,"invalid syntax.")
return
endIf
$engine = stringLeft($str,$delimiter-1)
$text = stringMid($str,$delimiter+1)
$result = call("speakWithEngine"&$engine,$text)
if @error then
dSend($id,"engine not implemented.")
return
endIf
if $result = -1 then ;error
dSend($id,"object not found.")
else
dSend($id,"ok.")
endIf
endFunc
addCmd("cmdn","runs a command without caring about the output.")
func lin_cmdn ($id, $str)
$pid = run(@comspec & " /C "&$str,"",@SW_HIDE)
dsend($id,"PID: "&$PID)
endFunc

addcmd("touch","touch: Creates a new file or zeros out a preexisting file. Usage: touch <file>")
Func lin_touch($id,$str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"touch: format not correct")
Return
EndIf
if FileExists($str) Then
$time = @Year&@Mon&@MDay&@HOUR&@MIN&@SEC
FileSetTime($str,$Time,1)
FileSetTime($str,$Time,2)
dSend($id,"touch: "&$str&": File was accessed")
Else
if FileWrite($str,"") Then
dSend($id,"touch: "&$str&": File created")
Else
dSend($id,"touch: "&$str&": Can not create file")
EndIf
EndIf
EndFunc
addcmd("fwrite","Writes to a text file. "&@lf&"Usage: filename|text to write. Yes you really include the |. ")
Func lin_fwrite($id,$str)
$fname = StringLeft($str,StringInStr($str,"|")-1)
$ftext = StringTrimLeft($str,StringLen($fname)+1)
if $fname = "" or $ftext = "" or StringRegExp($fname,"^com\d*$",0) or stringregexp($fname,"\\com\d*",0) then
dSend($id,"fwrite: "&$str&": Format not correct")
Else
$ftext = StringReplace($ftext,"\n",@LF)
$ftext = StringReplace($ftext,"\"&@LF,"\n")
$ftext = StringReplace($ftext,"\r",@CR)
$ftext = StringReplace($ftext,"\"&@CR,"\r")
if FileWrite($fname,$ftext) Then
dSend($id,"fwrite: "&$fname&": The file was written to")
Else
dSend($id,"fwrite: "&$fname&": Cannot write to the file")
EndIf
EndIf
EndFunc
addcmd("wc","Get's the size of a file. Usage wc <filename>.")
Func lin_wc($id,$str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"Usage: wc <file>")
return
EndIf
if FileExists($str) Then
dSend($id,$str&" is "&FileGetSize($str)&" bytes.")
Else
dsend($id,"Cannot find "&$str)
EndIf
EndFunc

addcmd("mv","Moves or renames a file."&@LF&"Usage: ""Input file"" ""Output file."""&@LF&"The character seperating the two files is not inportant but filenames must be wrapped in quotes even if they do not contain spaces.")
func lin_mv($id,$str)

$mv = _ParseQuotes($str)
if @Error Then
dsend($id,"Error: Quotes are not ballenced.")
ElseIf $mv[0] < 2 Then
dsend($id,"Incorrect number of arguments."&@lf&"Type help mv for more information.")
ElseIf $mv[1] = "" or StringRegExp($mv[1],"^com\d*$",0) or stringregexp($mv[1],"\\com\d*",0) or $mv[2] = "" or StringRegExp($mv[2],"^com\d*$",0) or stringregexp($mv[2],"\\com\d*",0) then
dSend($id,"mv: Error: Format not correct.")
ElseIf not FileExists($mv[1]) Then
dsend($id,"Cannot find "&$mv[1])
Else
$get = FileMove($mv[1],$mv[2],9)
if not $get Then
dsend($id,"Cannot move "&$mv[1]&" to "&$mv[2]&". Either that destination is invalid or you are not allowed to move files there.")
Else
dsend($id,$mv[1]&" was successfully moved to "&$mv[2])
EndIf
EndIf

EndFunc

addcmd("cp","Copies a file."&@LF&"Usage: ""Input file"" ""Output file."""&@LF&"The character seperating the two files is not inportant but filenames must be wrapped in quotes even if they do not contain spaces.")
func lin_cp($id,$str)

$cp = _ParseQuotes($str)
if @Error Then
dsend($id,"Error: Quotes are not ballenced.")
ElseIf $cp[0] < 2 Then
dsend($id,"Incorrect number of arguments."&@lf&"Type help cp for more information.")
ElseIf $cp[1] = "" or StringRegExp($cp[1],"^com\d*$",0) or stringregexp($cp[1],"\\com\d*",0) or $cp[2] = "" or StringRegExp($cp[2],"^com\d*$",0) or stringregexp($cp[2],"\\com\d*",0) then
dSend($id,"cp: error: Format not correct.")
ElseIf not FileExists($cp[1]) Then
dsend($id,"Cannot find "&$cp[1])
Else
$get = FileCopy($cp[1],$cp[2],9)
if not $get Then
dsend($id,"Cannot copy "&$cp[1]&" to "&$cp[2]&". Either that destination is invalid or you are not allowed to copy files there.")
Else
dsend($id,$cp[1]&" was successfully copied to "&$cp[2])
EndIf
EndIf

EndFunc

addcmd("kd", "sets key delay.")
func lin_kd($id, $str)
if stringIsInt($str) then
opt("sendKeyDelay", $str)
dSend($id, "set.")
else
dSend($id, "error.")
endIf
endFunc
addcmd("reg", "Usage: reg <cmd> <args>."&@lf&"Supported reg commands: "&@LF&"delete <key>|<value_name>"&@lf&"add <key>|<value_name>|[Data_Type]|[Value]"&@LF&"read <key>|<value_Name>")
func lin_reg($id, $str)
$r = stringRegExp($str, "^\s*(\w+)\s+(.+)", 1)
if @error then
dSend($id, "usage: reg <command> <args>")
return
endIf
call("reg_"&$r[0], $id, $r[1])
endFunc
func reg_delete($id, $str)
$bar = stringInStr($str, "|")
if $bar then
$r = regDelete(stringLeft($str, $bar-1), stringTrimLeft($str, $bar))
$e = @error
else
$r = regDelete($str)
$e = @error
endIf
if $r then
dSend($id, "Reg delete successfull")
else
dSend($id, "Reg delete failed")
endIf
endFunc

func reg_add($id, $str)
$r = ""
$e = ""
$arg = stringRegExp($str, "^(.+)\|(.+)\|(.+)\|(.*)",1)
if not @Error Then
$r = regWrite($arg[0], $arg[1], $arg[2], $arg[3])
$e = @error
ElseIf $str <> "" Then
$r = RegWrite($Str)
endIf
if $r then
dSend($id, "Reg add successfull")
else
dSend($id, "Reg add failed")
endIf
endFunc
Func Reg_Read($id,$str)
$r = ""
$bar = StringInStr($str,"|")
if $bar Then
$r = RegRead(StringLeft($str,$bar-1),StringTrimLeft($str,$bar))
EndIf
if $r <> "" Then
dSend($id,"Reg Read: "&$r)
Else
dSend($id,"Reg read failed.")
EndIf
EndFunc

AddCMD("she","Allows you to run an external program using the shell API. Usage: she <verb> <file name> [<parameters>]. "&@CRLF&"Verb: the action to perform on the nfile. Some include open, edit, print, and propperties (All of these are user interactive)."&@CRLF&"File name: The name of the file. If the file name has spaces, it must be in quotes."&@CRLF&"Parameters [optional]: Parameters to run the program with if needed.")
Func lin_she($id,$str)
if not StringRegExp($str,"^\w+\s+.+$",0) Then
DSend($ID,"Usage: she <verb> <file name> [<parameters>]. "&@CRLF&"Verb: the action to perform on the nfile. Some include open, edit, print, and propperties (All of these are user interactive)."&@CRLF&"File name: The name of the file. If the file name has spaces, it must be in quotes."&@CRLF&"Parameters [optional]: Parameters to run the program with if needed.")
Return
EndIf
$str &= " "
$verb = StringLeft($str,StringInStr($str," ")-1)
$str = StringTrimLeft($str,StringLen($verb)+1)
if StringLeft($str,1) = """" Then
$str = StringTrimLeft($str,1)
$fname = StringLeft($str,StringInStr($str,"""")-1)
$str = StringTrimLeft($str,StringLen($fname)+1)
Else
$fname = StringLeft($str,StringInStr($str," ")-1)
$str = StringTrimLeft($str,StringLen($fname)+1)
EndIf
$str = StringTrimRight($str,1)
if $verb <> "open" and $verb <> "edit" and $verb <> "print" and $verb <> "properties" Then
dSend($ID,"Error: "&$verb&" is not a valid verb.")
Return
EndIf
if $fname = "" or StringRegExp($fname,"^com\d*$",0) or stringregexp($fname,"\\com\d*",0) then
dSend($id,"she: Format not correct.")
return
EndIf
if ShellExecute($fname,$str,"",$verb) = 1 Then
dSend($ID,"The Shell execute function was successful.")
Else
dSend($ID,"The shell execute function was not successful.")
EndIf

EndFunc
AddCMD("cmdls","Gives you a list of tasks you have started with cmd. Note: This will not pick up the tasks started with cmdn nor will it see the ones that you start with ""cmd start"" or ""she open"".")
Func lin_cmdls($id,$str)
dSend($id,"Task list:")
for $i = 0 to uBound($Processes)-1
if $id = $Processes[$i][0] Then dSend($id,$Processes[$i][1]&" ("&$Processes[$i][2]&")")
Next
EndFunc
AddCmd("CMDSend","Allows you to send input to a process started with cmd. Usage: cmdsend <PID> <data>. To send CR, use \r, and to send LF, use \n. A line break will not be sent when the CMDSend command is sent.")
Func lin_CMdSend($id,$str)
$PID = StringLeft($Str,StringInStr($Str," ")-1)
if not StringIsInt($PID) Then
dSend($Id,"Invalid parameters.")
Return
EndIf
$Str = StringTrimLeft($Str,StringLen($PID)+1)

$str = StringReplace($str,"\r",@CR)
$str = StringReplace($str,"\n",@LF)
$f = 0
for $i = 0 to uBound($Processes)-1
if $PID = $Processes[$i][1] and ProcessExists($Processes[$i][1]) and $id = $Processes[$i][0] Then
STDInWrite($PID,$Str)
dSend($id,"Text sent to PID "&$PID)
$f = 1
ExitLoop
EndIf
Next
if $f = 0 Then dSend($id,"Either that process doesn't exist, it wasn't started by cmd or you do not have permition to send to that process.")
EndFunc
AddCmd("WinSetTitle","Sets the title of the active window. Usage: WinSetTitle <title>.")
Func lin_WinSetTitle($id,$str)
$Title = WinGetTitle("")
if WinSetTitle("","",$str) Then
dSend($id,"The Window title was changed from "&$Title&" to "&$str&".")
Else
dSend($id,"The Window title could not be changed. The title of the current window is "&$Title&".")
EndIf
EndFunc
addcmd("execute", "executes autoit code.")
func lin_execute($id, $str)
local $lin_result
$lin_result = execute($str)
if @error then
dSend($id, "error")
return
endIf
dSend($id, "result: "&$lin_result)
endFunc
addcmd("runs","runs: Runs an autoit script. Usage: runs <file>.")
func lin_runs($id,$str)
if not @Compiled Then
dSend($id,"runs: You must compile from source before using this command")
return
EndIf
if $str = "" Then
dSend($id,"Runs: Format incorrect")
return
EndIf
if not FileExists($str) Then
dSend($id,"runs: "&$str&": File doesn't exist")
return
EndIf
Run(@ScriptFullPath&" /autoit3executescript "&$str)
EndFunc
addcmd("winclose","Closes a window. Usage: winclose <window>")
Func lin_winclose($id,$str)
if $str = "" Then
dSend($id,"Usage: winclose <window>")
return
EndIf
if WinClose($str) Then
dSend($id,"Winclose: "&$str&" was closed.")
Else
dSend($id,"Winclose: "&$str&" doesn't exist.")
EndIf
EndFunc
addcmd("wincloseall","Closes all instances of a window. Usage: wincloseall <window>")
Func lin_wincloseall($id,$str)
if $str = "" Then
dSend($id,"Usage: wincloseall <window>")
return
EndIf
if WinClose($str) Then
$c = 1
While $c = 1
$c = WinClose($str)
Wend
dSend($id,"Wincloseall: "&$str&" was closed.")
Else
dSend($id,"Wincloseall: "&$str&" doesn't exist.")
EndIf
EndFunc
addcmd("winkill","Kills a window. Usage: winkill <window>")
Func lin_winkill($id,$str)
if $str = "" Then
dSend($id,"Usage: winkill <window>")
return
EndIf
$wpid = WinGetProcess($str)
if $wpid > -1 Then
ProcessClose($wpid)
dSend($id,"winkill: "&$str&" was killed.")
Else
dSend($id,"winkill: "&$str&" doesn't exist.")
EndIf
EndFunc
addcmd("winkillall","Kills all instances of a window. Usage: winkillall <window>")
Func lin_winkillall($id,$str)
if $str = "" Then
dSend($id,"Usage: winkillall <window>")
return
EndIf
$wpid = winGetProcess($str)
if $wpid > -1 Then
ProcessClose($wpid)
$c = 1
While $c >= 0
$c = WinGetProcess($str)
ProcessClose($c)
Wend
dSend($id,"winkillall: "&$str&" was killed.")
Else
dSend($id,"winkillall: "&$str&" doesn't exist.")
EndIf
EndFunc
addcmd("winactivate","activates a window. Usage: winactivate <window>")
Func lin_Winactivate($id,$str)
if $str = "" Then
dSend($id,"Usage: winactivate <window>")
return
EndIf
if Winactivate($str) Then
dSend($id,"Winactivate: "&$str&" was activated.")
Else
dSend($id,"Winactivate: "&$str&" doesn't exist.")
EndIf
EndFunc
addcmd("wls","Lists all of the windows that are visible. Usage: wls [title]. If title is specified, all visible windows who's title's have the title specified in them will be listed. Otherwise, all visible windows will be listed.")
Func lin_wls($id,$str)
if $str <> "" Then
$array = WinList($str)
Else
$array = WinList()
EndIf
$win = ""
for $i = 1 to $array[0][0]
if bitand(WInGetState($array[$i][1]),2) Then $win &= $array[$i][0]&@Lf
Next
dSend($id,"Window List: "&@LF&$win)
EndFunc
addcmd("getip","Gets the public IP of this computer.")
Func lin_getip($id,$str)
$diff = TimerDiff($ipcache[0])
if $diff > 15000 or $ipcache[1] = "" Then
$ips = _INetGetSource("http://checkip.dyndns.org:8245")
if @Error Then
$ips = _INetGetSource("http://checkip.dyndns.org")
if @Error Then
dSend($id,"getip: could not make an outgoing connection.")
return
EndIf
EndIf
$re = StringRegExp($ips,"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})",1)
if @Error Then
dSend($id,"getip: Cannot retreive IP from server.")
return
EndIf
$IPCache[0] = TimerInit()
$diff = 0
$IPCache[1] = $re[0]
$IPADDR = $re[0]
Else
$IPADDR = $IPCache[1]
EndIf
if Floor($diff/1000) = 1 Then
$sec = "second"
Else
$sec = "seconds"
EndIf
dSend($id,"getip: Public IP Address: "&$IPADDR&" (cached "&Floor($diff/1000)&" "&$sec&" ago).")
EndFunc
addcmd("getscreen","Gets a screen shot of the entire screen. Usage: getscreen <image>. The image will be saved in the format specified by the extention of the file. Valid formats for Windows XP are bmp, gif, jpeg, png and tif.")
Func lin_getscreen($id,$str)
if $str = "" or StringRegExp($str,"^com\d*$",0) or stringregexp($str,"\\com\d*",0) then
dSend($id,"Usage: getscreen <image>. The image will be saved in the format specified by the extention of the file. Valid formats for Windows XP are bmp, gif, jpeg, png and tif.")
return
EndIf
_screencapture_capture($str,0,0,-1,-1,true)
if FileExists($str) Then
dSend($id,"getscreen: Your image has been saved as "&$str)
Else
dSend($id,"getscreen: Cannot save image to "&$str)
EndIf
EndFunc
addcmd("dns","Get's the IP of a hostname. Usage: dns <hostname>.")
Func lin_dns($id,$str)
if $str = "" Then
dSend($id,"Usage: dns <hostname>.")
return
EndIf
if StringRegExp($str,"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") Then
$result = _TCPIpToName($str)
Else
$result = TCPNameToIP($str)
EndIf
dSend($id,"dns: "&$str&": "&$result)
EndFunc
addcmd("grep","Displays the lines in a file containing the specified string. Usage: Filename|string.")
Func lin_grep($id,$str)
$instr = StringInStr($str,"|")
$file = StringLeft($str,$instr-1)
$str = StringTrimLeft($str,$instr)
if $file = "" or StringRegExp($file,"^com\d*$",0) or stringregexp($file,"\\com\d*",0) then
dSend($id,"grep: Invalid filename")
return
EndIf
if not FileExists($file) Then
dSend($id,"grep: "&$File&": File doesn't exist.")
return
EndIf
if $str = "" Then
dSend($id,"grep: No string specified.")
return
EndIf
$sp = StringSplit(FileRead($File,FileGetSize($File)),@lf)
for $i = 1 to $sp[0]
$sp[$i] = StringRegExpReplace($sp[$i],"\r$","")
if StringRegExp($sp[$i],$str,0) Then dSend($id,$sp[$i])
Next
EndFunc
addcmd("uptime","Gets the uptime of Windows.")
func lin_uptime($id,$str)
$uptime = dllcall("kernel32.dll","long","GetTickCount")
$uptime = $uptime[0]
$Hours = Floor($uptime/3600000)
$mins = Floor(($uptime-($hours*3600000))/60000)
$Secs = Floor(($uptime-(60000*$Mins)-(3600000*$Hours))/1000)
$Days = Floor($hours/24)
$Hours = Mod($Hours,24)
$daym = "days"
$hourm = "hours"
$minm = "minutes"
$secm = "seconds"
if $days = 1 Then $daym = "day"
if $hours = 1 Then $hourm = "hour"
if $mins = 1 Then $minm = "minute"
if $secs = 1 Then $secm = "second"
dSend($id,"up for "&$days&" "&$daym&", "&$Hours&" "&$hourm&", "&$mins&" "&$minm&" and "&$secs&" "&$secm)
EndFunc
Addcmd("enableconsole","Enables or disables a program's ability to output to the console."&@LF&"Usage: enableconsole -e|-d<file>."&@LF&" -e enables the console. -d disables the console. <file> is the file to perform the operation on.")
Func lin_enableconsole($id,$str)
$re = StringRegExp($str,"^-(e|d)(.+)$",1)
if @Error Then
dSend($id,"Usage: enableconsole -e|-d<file>."&@LF&" -e enables the console. -d disables the console. <file> is the file to perform the operation on.")
return
EndIf
$action = $re[0]
$File = $re[1]
if $action = "e" Then
dSend($id,"Enable console for "&$File)
Else
dSend($id,"Disable console for "&$File)
EndIf
if not FileExists($File) Then
dSend($id,"File doesn't exist.")
return
EndIf
		$hRead = FileOpen($File, 16)
		$bR = FileRead($hRead)
		FileClose($hRead)
		If BinaryMid($bR, 1, 2) <> "MZ"  Then
dSend($id,"File is not an executable.")
return
		EndIf
		$e_lfanew = Dec(Hex(Binary(BitRotate(String(BinaryMid($bR, 61, 4)), 32, "D"))))
		If BinaryMid($bR, $e_lfanew + 1, 2) <> "PE"  Then
dSend($id,"PE header not found.")
return
		EndIf
		If BinaryMid($bR, $e_lfanew + 24 + 1, 2) <> "0x0B01"  Then
dSend($id,"Optional header not found.")
return
		EndIf
if $action = "e" Then
		If BinaryMid($bR, $e_lfanew + 24 + 68, 2) <> "0x0003" Then
			$new = BinaryMid($bR, 1, $e_lfanew + 24 + 68) & Binary("0x0300") & BinaryMid($bR, $e_lfanew + 24 + 68 + 2 + 1)
			$hWrite = FileOpen($file, 18)
			FileWrite($hWrite, $new)
if @Error Then
dSend($id,"Could not write data.")
Else
dSend($id,"Done.")
EndIf
			FileClose($hWrite)
Else
dSend($id,"Console has already been opened.")
endIf
Else
		If BinaryMid($bR, $e_lfanew + 24 + 68, 2) <> "0x0002" Then
			$new = BinaryMid($bR, 1, $e_lfanew + 24 + 68) & Binary("0x0200") & BinaryMid($bR, $e_lfanew + 24 + 68 + 2 + 1)
			$hWrite = FileOpen($file, 18)
			FileWrite($hWrite, $new)
if @Error Then
dSend($id,"Could not write data.")
Else
dSend($id,"Done.")
EndIf
			FileClose($hWrite)
Else
dSend($id,"Console has already been closed.")
endIf
EndIf
EndFunc
